/**
 * @filename	UploadData.java
 * @date		2016年7月19日-上午11:12:50
 * @author		dhy
 */
package com.android.functionmanagement.util;

import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.globle.LogGloble;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

/**
 * @author dhy
 * 
 */
public final class UploadData extends JSONObject {

	/**
	 * 
	 * @param context
	 * @param type
	 *            1-计步/2-心率/3-定位/4-sos（定位+声音文件）/5-开关机/6-低电
	 * @param loc_type
	 *            定位类型,0 gps,1,基站(lbs)，2，wifi。
	 */

	public UploadData(Context context, int type, int pos_type) {
		final String userId = SharedPreferencesUtils.getParam(context,ConstantGloble.USERID, "").toString();
		if("".equals(userId))
			return;
		final String deviceId = DeviceUtils.getIMEI(context);
		final String takeTime = DeviceUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss");
		try {
			put(ConstantGloble.USERID, userId);
			put(ConstantGloble.DEVICEID, deviceId);
			put("takeTime", takeTime);
			put("type", "" + type);
			switch (type) {
			case ConstantGloble.UPLOAD_TYPE_OF_STEP:
				final String steps = getStepCount(context) + "";
				put("steps", steps);
				final HashMap<Long, Integer> stepMaps = StepOpenHelper.getInstance(context).getStepBufferHashMap();
				if ((stepMaps != null) && (stepMaps.size() > 0)) {
					String jsonStr = JSON.toJSONString(stepMaps);
					put("stepsHashMap", jsonStr);
					LogGloble.d("fu_step", " =UploadData="+"=steps:"+steps+"=stepMaps.size: " + stepMaps.size() + "==stepMaps:"+jsonStr);
				}
				StepOpenHelper.getInstance(context).deleteStepBuffer();
				break;
			case ConstantGloble.UPLOAD_TYPE_OF_HR:
				final String heartRate = (String) SharedPreferencesUtils
						.getParam(context, ConstantGloble.HEARTRATE_DATA, "80");
				put("heartRate", heartRate);
				break;
			case ConstantGloble.UPLOAD_TYPE_OF_SOS:
				final String fileName = (String) SharedPreferencesUtils
						.getParam(context, "fileName", "80");
				put("fileName", fileName);
			case ConstantGloble.UPLOAD_TYPE_OF_LOCATION:
				final String longitude = (String) SharedPreferencesUtils
						.getParam(context, "longitude", "80");
				final String latitude = (String) SharedPreferencesUtils
						.getParam(context, "latitude", "80");
				final String lac = (String) SharedPreferencesUtils.getParam(
						context, "lac", "80");
				final String mcc = (String) SharedPreferencesUtils.getParam(
						context, "mcc", "80");
				final String mnc = (String) SharedPreferencesUtils.getParam(
						context, "mnc", "80");
				final String cid = (String) SharedPreferencesUtils.getParam(
						context, "cid", "80");
				final String cell_rss = (String) SharedPreferencesUtils
						.getParam(context, "cell_rss", "80");
				final String nb_count = (String) SharedPreferencesUtils
						.getParam(context, "nb_count", "80");
				final String nb_lac = (String) SharedPreferencesUtils.getParam(
						context, "nb_lac", "80");
				final String nb_cid = (String) SharedPreferencesUtils.getParam(
						context, "nb_cid", "80");
				final String nb_rxl = (String) SharedPreferencesUtils.getParam(
						context, "nb_rxl", "80");
				final String wifi_count = (String) SharedPreferencesUtils
						.getParam(context, "wifi_count", "80");
				final String wifi_aps = (String) SharedPreferencesUtils
						.getParam(context, "wifi_aps", "80");
				final String wifi_si = (String) SharedPreferencesUtils
						.getParam(context, "wifi_si", "80");
				put("pos_type", "" + pos_type);
				switch (pos_type) {
				case 0:
					put("longitude", longitude);
					put("latitude", latitude);
					break;
				case 2:
					put("wifi_count", wifi_count);
					put("wifi_aps", wifi_aps);
					put("wifi_si", wifi_si);
				case 1:
					put("lac", lac);
					put("mcc", mcc);
					put("mnc", mnc);
					put("cid", cid);
					put("cell_rss", cell_rss);
					put("nb_count", nb_count);
					put("nb_lac", nb_lac);
					put("nb_cid", nb_cid);
					put("nb_rxl", nb_rxl);
					break;
				default:
					break;
				}

				break;

			case ConstantGloble.UPLOAD_TYPE_OF_POWER:
				final String on_off = (String) SharedPreferencesUtils.getParam(
						context, "on_off", "1");
				put("on_off", on_off);
				break;
			case ConstantGloble.UPLOAD_TYPE_OF_LOW_BAT:
				final String lowBattery = (String) SharedPreferencesUtils
						.getParam(context, "lowBattery", "80");
				put("lowBattery", lowBattery);
				break;
			default:
				break;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
	private int getStepCount(Context context) {
		int step = StepOpenHelper.getInstance(context).getStepCount(getCurrentDate());
		return step;
	}

	private String getCurrentDate() {
		return df.format(System.currentTimeMillis());
	}
}
